<template>
  <div>
    <AForm :model="searchFrom" class="erp-search-form">
      <ARow class="grid-demo" :gutter="10">
        <ACol :span="6">
          <AFormItem hide-label>
            <ARangePicker
              v-model="searchFrom.dates"
              :placeholder="['开始月份', '结束月份']"
              value-format="YYYY-MM-DD"
              :allow-clear="false"
            />
          </AFormItem>
        </ACol>
      </ARow>
    </AForm>
    <ACard>
      <div class="erp-mb-10 w100 erp-search-btn-box">
        <ASpace>
          <AButton type="primary" @click="onSearch">
            <template #icon>
              <icon-search />
            </template>
          </AButton>
          <ATooltip content="清空筛选项" position="top">
            <AButton type="dashed" @click="onReset">
              <template #icon>
                <icon-refresh />
              </template>
            </AButton>
          </ATooltip>
        </ASpace>
        <!-- <ASpace>
          <AButton type="primary" @click="onExport('table')">导出</AButton>
        </ASpace> -->
      </div>
      <ATable
        ref="defineTableRef"
        :data="TableData"
        :bordered="{ cell: true }"
        column-resizable
        :scroll="{ maxHeight: 300 }"
        row-key="seq"
        :hide-expand-button-on-empty="true"
        :pagination="false"
      >
        <template #columns>
          <ATableColumn :width="50"> </ATableColumn>
          <ATableColumn title="项目名称" data-index="itemName" :width="100">
            <ATableColumn>
              <template #cell="{ record }">
                {{ record.itemName }}
              </template>
            </ATableColumn>
            <ATableColumn>
              <template #cell="{ record }">
                {{ record.accountName }}
              </template>
            </ATableColumn>
          </ATableColumn>
          <ATableColumn title="总金额" data-index="amt" :width="100">
            <template #cell="{ record }">
              {{ `¥${formatPrice(record.amt)}` }}
            </template>
          </ATableColumn>
          <ATableColumn title="备注" data-index="remark"></ATableColumn>
        </template>
      </ATable>
    </ACard>
  </div>
</template>

<script lang="ts" setup>
import { TableInstance } from '@arco-design/web-vue'
import {
  FinanceTotalAssetStatRes,
  FinanceTotalAssetStatSearch,
  getFinanceTotalAssetStat
} from '@renderer/api/financialSheet'
import { formatPrice } from '@renderer/utils/mixins'
import dayjs from 'dayjs'

const searchFrom = ref<FinanceTotalAssetStatSearch>({
  dates: [
    dayjs().startOf('year').format('YYYY-MM-01'),
    dayjs().endOf('month').format('YYYY-MM-DD')
  ],
  startDate: '',
  endDate: ''
})
const TableData = ref<FinanceTotalAssetStatRes[]>([])
const defineTableRef = ref<TableInstance>()

onMounted(() => {
  getTableData()
})

async function getTableData() {
  const [createdStartDate, createdEndDate] = searchFrom.value.dates! || []
  searchFrom.value.startDate = createdStartDate
  searchFrom.value.endDate = createdEndDate
  const res = await getFinanceTotalAssetStat({ ...searchFrom.value })
  TableData.value = res.data.list.map((item) => {
    item.children = item.itemList
    return item
  })
  nextTick(() => {
    defineTableRef.value?.expandAll(true)
  })
}

function onSearch() {
  getTableData()
}
function onReset() {
  searchFrom.value = {
    dates: [
      dayjs().startOf('year').format('YYYY-MM-01'),
      dayjs().endOf('month').format('YYYY-MM-DD')
    ],
    startDate: '',
    endDate: ''
  }
  getTableData()
}
</script>
<style lang="less" scoped>
.erp-search-btn-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
